// director/application/head-application/head-application.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var Map = require("../../../utils/map.js");

// 获取地图的返回信息
const chooseLocation = requirePlugin('chooseLocation');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        location: {
            latitude: 0,
            longitude: 0
        },
        mapName: "",
        mapDetail: ""
	},

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const location = chooseLocation.getLocation();
        if(location){
            let mapName = location.province + location.city + location.district;
            
            console.log("123");
            if(!mapName){
                mapName = location.address.split(location.name)[0] || ""
            }
            const mapDetail = location.address.split(location.district)[1] + location.name;
            this.setData({
                location,
                mapName: mapName || "",
                mapDetail: mapDetail || ""
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        chooseLocation.setLocation(null);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
	formSubmit: function(e){
        // wx.showLoading({
        //     title: "正在加载"
        // })
        console.log("e", e.detail.value);
        let value = e.detail.value;
        const result = this.validForm(value);
        if(result){
            // 验证成功
            value.storeAddress = this.data.mapName + this.data.mapDetail;
            const data = this.getParams(value);
            this.applyMember(data);
        }
        
    },
    // 表单验证
    validForm(values){
        let keys = ["storeName", "village", "storeAddress"];//必填选项
        const { hasStorefront, location, back, positive } = this.data;
        const result = keys.every(item => values[item]);
        if(!result){
            wx.showToast({
                title: "还有选项没填哦",
                icon: "none"
            })
            return false;
        }
        if(location.latitude == 0){
            wx.showToast({
                title: "所在位置还没选哦",
                icon: "none"
            })
            return false;
        }
        return true;
    },
    // 参数处理
    getParams(values){
        const { location, back, positive } = this.data;
        const userInfo = wx.getStorageSync("userInfo");
        let defaultData = {
            "isSupportFreeze": true,
            "lat": location.latitude,
            "lon": location.longitude,
            "memberLeaderId": userInfo.id || 0,
            "status": 0,
            "storeAddress": "",
            "storeImage": "",
            "storeName": "",
            "village": "",
            "workerMemberId": 0
        };
        values.isSupportFreeze = values.isSupportFreeze == 1 ? true : false;
        return Object.assign(defaultData, values)
    },
    // 后台请求
    applyMember(data){
        wx.showLoading({
            title: "正在提交中",
            icon: "none"
        })
        util.request(
            api.leader.addStoreInfoUrl,
            data,
            "POST"
        ).then(res=>{
            console.log("ress", res);
            wx.hideLoading();
            wx.showToast({
                title: res.message,
                icon: "none"
            })
            wx.navigateBack();
        }).catch(err=>{
            console.log("err", err);
            wx.hideLoading();
            wx.showToast({
                title: err.message,
                icon: "none"
            })
        })
    },
    // 地图事件
    toMap(){
        Map.getAddress(this.data.location);
    },
});
